/*
 * HistoryActivity.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.history;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;

import de.studiorutton.gerengdic.Prefs;
import de.studiorutton.gerengdic.R;
import de.studiorutton.gerengdic.dictionary.DictionaryActivity;
import de.studiorutton.gerengdic.indexeddictionary.IndexedDictionaryActivity;


public class HistoryActivity extends ListActivity {
	private final static String TAG = "learningwords";
	
	private History h;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		Log.d( TAG, "HistoryActivity: onCreate." );
		setContentView( R.layout.historylist );
	}
	
	@Override
	protected void onStart() {
		super.onStart();
//		Log.d( TAG, "HistoryActivity: onStart." );

		h = History.get();
		LinearLayout ll = (LinearLayout) findViewById( R.id.history );
		ll.setBackgroundColor( Prefs.getInstance().getColorSchemeMapper().getBackgroundColor());
		
		getListView().setDivider( Prefs.getInstance().getColorSchemeMapper().getListDividerDrawable());
	}

	@Override
	protected void onResume() {
		super.onResume();
//		Log.d( TAG, "HistoryActivity: onResume." );
		
		updateHistoryList();

	}
	
	@Override
	protected void onPause() {
		super.onPause();
//		Log.d( TAG, "HistoryActivity: onPause." );

		if (isFinishing()) {
//			Log.d( TAG, "HistoryActivity: isFinishing." );
			h.closeCursor();
			History.release();
			h = null;
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
//		Log.d( TAG, "HistoryActivity: onStop." );
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
//		Log.d( TAG, "HistoryActivity: onDestroy." );
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.searchhistorymenu, menu);
		return true;
	}

	public void updateHistoryList() {
		setListAdapter(h.getHistoryCursorAdapter( this ));
		getListView().setOnItemClickListener(onHistoryClickListener);
	}
	
	private OnItemClickListener onHistoryClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> historyList, View arg1,
				int position, long arg3) {
//			Log.d(TAG, "OnAutoCompleteListener: clicked:" + position);
			
			Cursor cursor = (Cursor) historyList.getItemAtPosition( position );
			
			String searchString = cursor.getString( cursor.getColumnIndex( HistoryProvider.SEARCH_STRING));
			int searchWay = cursor.getInt( cursor.getColumnIndex( HistoryProvider.SEARCH_WAY));
			int searchLanguage = cursor.getInt( cursor.getColumnIndex( HistoryProvider.SEARCH_LANGUAGE ));
			
			ICommand command;
			if ( searchWay == AbstractCommand.SEARCH_WAY_FULLTEXT )
				command = new DictionaryCommand(searchString, searchLanguage );
			else
				command = new IndexedDictionaryCommand( searchString, searchLanguage );
			
			command.execute( HistoryActivity.this );

		}
	};
	
	public static final int DATABASE_UPDATE = 10;
	public static final int DICTIONARY_RUN = 20;
	public static final int HISTORY = 30;
	
	public static final int DATABASE_UPDATE_CANCELED = 1;
	public static final int DATABASE_UPDATE_OK = 2;
	public static final int DICTIONARY_RETURN = 3;
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		Intent i;
	    switch (item.getItemId()) {
	    case R.id.menudictionary:
	    	i = new Intent( getApplicationContext(), DictionaryActivity.class );
			startActivityForResult( i, DICTIONARY_RUN );
			return true;
	    case R.id.menuindexeddictionary:
	    	i = new Intent( getApplicationContext(), IndexedDictionaryActivity.class);
	    	startActivityForResult( i, DICTIONARY_RUN );
	    	return true;
	    case R.id.clearhistory:
	    	h.clearHistory();
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	public Context getContext() {
		return this;
	}
	
}
